package com.tianyu.common;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import lombok.extern.slf4j.Slf4j;
import okhttp3.*;
import org.springframework.stereotype.Component;

import jakarta.annotation.Resource;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.net.ConnectException;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;


/**********************************
 * @Author YSW
 * @Description
 * @Date 2022/4/26 - 18:20
 **********************************/

@Slf4j
@Component
public class HttpModel {

    @Resource
    private AppConstants appConstants;

    private static final String baseUrl = "";

    private static final OkHttpClient HTTP_CLIENT = new OkHttpClient().newBuilder()
            .callTimeout(5, TimeUnit.SECONDS)
            .protocols(Collections.singletonList(Protocol.HTTP_1_1))
            .build();

    public JSONObject sitesGet(String token) {
        Request request = new Request.Builder()
                .url(baseUrl + "/api/v3/sites/list")
                .header("token", token)
                .get()
                .build();
        try (final Response execute = HTTP_CLIENT.newCall(request).execute()) {
            if (execute.body() != null) {
                return JSONObject.parseObject(execute.body().string());
            }
            log.error("请求响应为空");
        } catch (ConnectException e) {
            log.error("HTTP服务异常");
        } catch (InterruptedIOException e) {
            log.error(e.getMessage());
        } catch (IOException e) {
            log.error(e.getMessage());
            e.printStackTrace();
        }
        return JSONObject.of();
    }

}
